package com.sofoscorp.android.gestcobranzamobile;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class RANGE_NUMBER_BO {
	private ConexionDB go_conexDB;
	private SQLiteDatabase go_db;
	private RANGE_NUMBER_Str gs_RANGE_NUMBER;
	private Cursor gc_RANGE_NUMBER;
	private String C_PATH = "/data/data/com.sofoscorp.android.gestcobranzamobile/databases/GestCobranzaMobileDB";
	
	public static final String C_TABNAME = "RANGE_NUMBER" ;
	
	public static final String C_COLUMNA_TYPOP = "TYPOP";
	public static final String C_COLUMNA_RANGE = "RANGE";
	public static final String C_COLUMNA_GJAHR = "GJAHR";
	public static final String C_COLUMNA_RGMIN = "RGMIN";
	public static final String C_COLUMNA_RGMAX = "RGMAX";
	public static final String C_COLUMNA_COUNT = "COUNT";
	
	String[] gt_campos = new String[]{ C_COLUMNA_TYPOP, C_COLUMNA_RANGE, C_COLUMNA_GJAHR, C_COLUMNA_RGMIN, C_COLUMNA_RGMAX, C_COLUMNA_COUNT} ;

	public RANGE_NUMBER_BO() {
		go_conexDB = new ConexionDB();
		//Pasar el Path de DB
		go_conexDB.serialization(C_PATH);
	}
		
	
	public RANGE_NUMBER_Str get_SingleByKey(String ivTYPOP, String ivRANGE, String ivGJAHR){
		String lv_Where = "TYPOP = ? AND RANGE = ? AND GJAHR = ?";
		String[] lt_WhereArgs;
		
		if(ivTYPOP.equals("") || ivTYPOP.equals(null) || 
		   ivRANGE.equals("") || ivRANGE.equals("") ||
		   ivGJAHR.equals("") || ivGJAHR.equals(null)){
			gs_RANGE_NUMBER = null;
		}else {
			gc_RANGE_NUMBER = null;
			lt_WhereArgs = new String[]{ivTYPOP,ivRANGE,ivGJAHR};
			
			//Pasar el Path de DB
			go_conexDB.serialization(go_conexDB.C_PATH_FROMSAP);

			//Verificar si existe conexion con la DB
			if (go_conexDB.checkConexDB()) {

				//Obtener la DB
				go_db = go_conexDB.getGo_DB();
				
				try {
					//Ejecutar Select a la DB
					gc_RANGE_NUMBER = go_db.query(C_TABNAME, gt_campos, lv_Where, lt_WhereArgs, null, null, null, null);
					if (gc_RANGE_NUMBER.moveToFirst()) {
						gs_RANGE_NUMBER = new RANGE_NUMBER_Str(	gc_RANGE_NUMBER.getString(0), 
																gc_RANGE_NUMBER.getString(1), 
																gc_RANGE_NUMBER.getString(2), 
																gc_RANGE_NUMBER.getString(3),
																gc_RANGE_NUMBER.getString(4),
																gc_RANGE_NUMBER.getInt(5));
					}else{
						gs_RANGE_NUMBER = null;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}else{
				gs_RANGE_NUMBER = null;
			}
		}

		go_db.close();
		gc_RANGE_NUMBER.close();
		return gs_RANGE_NUMBER;
	}
	
	
	public int updateRecord(RANGE_NUMBER_Str ivRecord) {
		ContentValues values = new ContentValues();
		
		//Verificar si existe conexion con la DB
		if (go_conexDB.checkConexDB()) {
			//Obtener la DB
			go_db = go_conexDB.getGo_DB();
			
			values.put(C_COLUMNA_COUNT, ivRecord.getCOUNT() + 1);
			//updating row
			return go_db.update(C_TABNAME, 
								values, 
								C_COLUMNA_TYPOP + " = ? AND "+ 
	    						C_COLUMNA_RANGE + " = ? AND "+
	    						C_COLUMNA_GJAHR + " = ?",
	    						new String[] { ivRecord.getTYPOP(), ivRecord.getRANGE(), ivRecord.getGJAHR()});
		}else{
			return 4;
		}
	}
}
